package com.mindainfo.configCenter.apk.web;

import com.mindainfo.configCenter.apk.service.IApkService;
import com.mindainfo.configCenter.apk.vo.ApkVo;
import com.mindainfo.configCenter.base.common.DataGrid;
import com.mindainfo.configCenter.base.common.Response;
import com.mindainfo.configCenter.base.common.ResponseFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * Created by MD-01 on 2016/11/17.
 */
@Controller
@RequestMapping("/apk")
public class ApkController {

    @Resource
    private IApkService apkService;

    @RequestMapping("/list")
    public String list(){
        return "apk/apkList";
    }

    @RequestMapping("/queryList")
    @ResponseBody
    public DataGrid<ApkVo> queryList(String keyword, Integer page, Integer rows){

        return apkService.queryList(keyword,page,rows);
    }

    @RequestMapping("/add")
    public String add(String name, String area,
                              Integer version, String type,
                              @RequestParam("file") MultipartFile file,
                              HttpServletRequest request){
        String scheme = request.getScheme();
        String host = request.getLocalAddr();
        int port = request.getLocalPort();
        String preStr = scheme+"://"+host+":"+port;
        Response<Void> result = apkService.add(name,area,version,type,file,preStr);
        return "redirect:/apk/list";
    }

    @RequestMapping("/addRest")
    @ResponseBody
    public Response<Void> addRest(String name, String area,
                              Integer version, String type,
                              @RequestParam("file") MultipartFile file,
                              HttpServletRequest request){
        String scheme = request.getScheme();
        String host = request.getLocalAddr();
        int port = request.getLocalPort();
        String preStr = scheme+"://"+host+":"+port;
        return apkService.add(name,area,version,type,file,preStr);
    }

    @RequestMapping("/del")
    @ResponseBody
    public Response<Void> del(String id){
        return apkService.del(id);
    }

    @RequestMapping("/addApk")
    public String addApk(){
        return "apk/addApk";
    }
}
